package com.example.demo.thread.loopPrint;

import javax.swing.plaf.TableHeaderUI;

public class Join_ABC {

    static class printABC implements Runnable{
        private Thread beforeThread;

        public printABC(Thread beforeThread){
            this.beforeThread = beforeThread;
        }

        @Override
        public void run() {
            if(beforeThread != null){
                try {
                    beforeThread.join();
                    System.out.println(Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }else{
                System.out.println(Thread.currentThread().getName());
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread a = new Thread(new printABC(null), "A");
            Thread b = new Thread(new printABC(a), "B");
            Thread c = new Thread(new printABC(b), "C");
            a.start();
            b.start();
            c.start();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
